package com.example.bill.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.bill.R;
import com.example.bill.adapter.MonthDayAdapter;
import com.example.bill.application.BillApplication;
import com.example.bill.entity.Memo;
import com.example.bill.repository.MemoRepository;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class AddBillFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_add_bill, container, false);

        MemoRepository dataBase = BillApplication.getBillApplication().getDataBase();

        Spinner dateSpinner = view.findViewById(R.id.dateSpinner);

        dateSpinner.setAdapter(new MonthDayAdapter(getContext()));
        dateSpinner.setSelection(0);

        EditText titleView = view.findViewById(R.id.title);

        Button buttonAdd = view.findViewById(R.id.btnAdd);

        View.OnFocusChangeListener nullValidator = (v, hasFocus) -> {
            // 校验逻辑
            if (!hasFocus) {
                EditText et = (EditText) v;
                if (et.getText().toString().isEmpty()) {

                    et.setError("不能为空");
                    buttonAdd.setEnabled(false);
                } else {
                    et.setError(null);
                    buttonAdd.setEnabled(true);
                }
            }
        };
        titleView.setOnFocusChangeListener(nullValidator);


        RadioGroup typeGroup = view.findViewById(R.id.typeGroup);
        typeGroup.check(R.id.outcome);

        EditText amountText = view.findViewById(R.id.amount);

        amountText.setOnFocusChangeListener(nullValidator);


        buttonAdd.setOnClickListener(v -> {

            nullValidator.onFocusChange(titleView, false);
            nullValidator.onFocusChange(amountText, false);

            if (amountText.getError() != null || titleView.getError() != null) {
                return;
            }

            Memo memo = new Memo();

            memo.setName(titleView.getText().toString());
            memo.setAmount(Double.parseDouble(amountText.getText().toString()));

            if (typeGroup.getCheckedRadioButtonId() == R.id.outcome) {
                memo.setType(1);
            } else {
                memo.setType(2);
            }

            LocalDateTime date = (LocalDateTime) dateSpinner.getSelectedItem();
            if (date != null) {
                memo.setDate(Date.from(date.atZone(ZoneId.systemDefault()).toInstant()));
            }
            String tip;
            if (dataBase.insert(memo) > 0) {
                tip = "成功";
            } else {
                tip = "失败";
            }
            Toast.makeText(getContext(), "保存" + tip, Toast.LENGTH_SHORT).show();
        });

        return view;
    }
}
